package com.bawei.serves;

import com.alibaba.nacos.client.utils.StringUtils;
import com.baidu.entity.Product;
import com.baidu.mapper.ProductMapper;
import com.baidu.utils.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lvchong
 * @Description: 修改
 * @Date: Created in 2021/12/13 9:48
 * @Modified By:
 */
@Service
@Transactional
public class ProductServerImpl implements ProductServer {

    @Resource
    private ProductMapper productMapper;
    @Override
    public ResultResponse list (Product product) {
        QueryWrapper<Product> wrapper=new QueryWrapper();
        if(product.getPname()!=null&&!product.getPname().equals("")){
            wrapper.like("pname","%"+product.getPname()+"%");
            List<Product> list = productMapper.selectList(wrapper);
            return ResultResponse.SUCCESS(list);
        }
        List<Product> products = productMapper.selectList(null);
        return ResultResponse.SUCCESS(products);
    }

    @Override
    public ResultResponse deletes (Product product) {
        product.setStatus(2);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",product.getPid());
        productMapper.update(product, wrapper);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse update (Product product) {
        product.setStatus(0);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",product.getPid());
        productMapper.update(product, wrapper);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse adds (Product product) {
        if(!StringUtils.isNotBlank(product.getPname())){
            return ResultResponse.FAILED("名称不可为空");
        }
        if(product.getRid()==null||product.getRid()==0){
            return ResultResponse.FAILED("产品利率不可为空");
        }
        if(product.getMid()==null||product.getMid()==0){
            return ResultResponse.FAILED("还款方式不可为空");
        }
        product.setStatus(0);
        productMapper.insert(product);
        return ResultResponse.SUCCESS("添加成功");
    }
}
